from __future__ import print_function

## Import list
from cli import CLI
import logging

class Tag(CLI):

	def add(self):
		if not self._args.tag:
			logging.critical("media descr requires --tag")
			raise self.CLIExceptionMissingArg("tag")
		try:
			for tag in self._args.tag:
				self._db.addTag(tag)
		except:
			raise

	def ls(self):
		try:
			tagList = self._db.getTagAndCountList()
			print("{:^4s} | {:^20s} | {:^03s}".format("ID", "Tag", "Media Count"))
			for row in tagList:
				print("{:4d} | {:20s} | {:03d}".format(row["id"], row["tag"], row["useCount"]), end = '')
				if row["useCount"] == 0:
					print(" EMPTY!", end = '')
				print("")
		except:
			raise

	def rm(self):
		if not self._args.tag and not self._args.id:
			logging.critical("media descr requires --tag and/or --id")
			raise self.CLIExceptionMissingArg("tag and/or id")
		try:
			for tag in self._args.tag:
				self._db.rmTag(tag)
		except TypeError as e:
			pass
		except:
			raise
		try:
			for tagID in self._args.id:
				self._db.rmTagByID(tagID)
		except TypeError as e:
			pass
		except:
			raise
